<?php

class RNotesView{
   
    
public static function get($database, $view, $username="", $password="", $server="9217-DOMKSPRD01", $antal=0){
	
	$output = null;
	$start = 1;
	
	do{
		if ($username!="" && $password!=""){
			$xmlsti = "http://$username:$password@$server:81/$database/$view?ReadViewEntries&count=1000&start=$start&preformat";
		}else{
                        $xmlsti = "http://$server:81/$database/$view?ReadViewEntries&count=1000&start=$start&preformat";
		}
		$xml = RXml::getArray($xmlsti);

$entries = $xml['VIEWENTRIES'][0]['VIEWENTRY'];

if ($antal == 0){
	$antal = (int)$xml['VIEWENTRIES'][0]['ATTRIBUTES']['TOPLEVELENTRIES'];
}

if (is_array($entries)){
	foreach ($entries as $e){
		
		$tmp = null;
		
		// Row - attributes
		$tmp['UNID'] = $e['ATTRIBUTES']['UNID'];
		
		// Row - data
		if (is_array($e['ENTRYDATA'])){
			foreach ($e['ENTRYDATA'] as $d){
				$colnavn = $d['ATTRIBUTES']['NAME']!=""?$d['ATTRIBUTES']['NAME']:$d['ATTRIBUTES']['COLUMNNUMBER'];
				$tmp[$colnavn] = $d['TEXT'][0]['VALUE'];				
			}
		}
		
		
		$output[] = $tmp;
		
	}
	
	$start = $start + 1000;
	if ($start > $antal){
		$start = 0; // THE END
	}
	
}else{
	
	$start = 0; // THE END
		
}
}while($start > 0);

return $output;

}
	


public static function getByKey($database, $view, $key, $username="", $password="", $server="9217-DOMKSPRD01", $antal=100){
	
	$output = null;
	$start = 1;
	
	
		if ($username!="" && $password!=""){
			$xmlsti = "http://$username:$password@$server:81/$database/$view?ReadViewEntries&startkey=$key&count=$antal&start=$start&preformat";
		}else{
                        $xmlsti = "http://$server:81/$database/$view?ReadViewEntries&startkey=$key&count=$antal&start=$start&preformat";
		}
		$xml = RXml::getArray($xmlsti);

$entries = $xml['VIEWENTRIES'][0]['VIEWENTRY'];

if (is_array($entries)){
	foreach ($entries as $e){
		
		$tmp = null;
		
		// Row - attributes
		$tmp['UNID'] = $e['ATTRIBUTES']['UNID'];
		
		// Row - data
		if (is_array($e['ENTRYDATA'])){
			foreach ($e['ENTRYDATA'] as $d){
				$colnavn = $d['ATTRIBUTES']['NAME']!=""?$d['ATTRIBUTES']['NAME']:$d['ATTRIBUTES']['COLUMNNUMBER'];
				$tmp[$colnavn] = $d['TEXT'][0]['VALUE'];				
			}
		}
		
		
		$output[] = $tmp;
		
	}
	
}
$output2 = null;
$key_col = "";
if (is_array($output)){
    foreach (array_keys($output[0]) as $col){
        if ($output[0][$col] == $key){
            if ($key_col == ""){
                $key_col = $col;
            }
        }
    }
}
if ($key_col == ""){
    return false;
}

if (is_array($output)){
    foreach ($output as $o){
        if ($o[$key_col] == $key){
            $output2[] = $o;
        }
    }
}



return $output2;

}


	
public static function toMySQL($mysqldb, $mysqltable, $notesdatabase, $view, $username="", $password="", $server="9217-DOMKSPRD01", $antal=0){
	
	$db = new dbtable($mysqldb, $mysqltable);
	
	$output = null;
	$start = 1;
	
	do{
		if ($username!="" && $password!=""){
			$xmlsti = "http://$username:$password@$server:81/$notesdatabase/$view?ReadViewEntries&count=1000&start=$start&preformat";
		}else{
			$xmlsti = "http://$server:81/$notesdatabase/$view?ReadViewEntries&count=1000&start=$start&preformat";
		}
		
		$xml = RXml::getArray($xmlsti);

$entries = $xml['VIEWENTRIES'][0]['VIEWENTRY'];

if ($antal == 0){
	$antal = (int)$xml['VIEWENTRIES'][0]['ATTRIBUTES']['TOPLEVELENTRIES'];
}

if (is_array($entries)){
	foreach ($entries as $e){
		
		$tmp = null;
		
		// Row - attributes
		$tmp['UNID'] = $e['ATTRIBUTES']['UNID'];
		
		// Row - data
		if (is_array($e['ENTRYDATA'])){
			foreach ($e['ENTRYDATA'] as $d){
				$colnavn = $d['ATTRIBUTES']['NAME']!=""?$d['ATTRIBUTES']['NAME']:$d['ATTRIBUTES']['COLUMNNUMBER'];
				$tmp[$colnavn] = $d['TEXT'][0]['VALUE'];				
			}
		}
		
		if (is_array($tmp)){
			$debug = $db->saverow($tmp);
			if (!$debug){
				return false;
			}
		}
		
	}
	
	$start = $start + 1000;
	if ($start > $antal){
		$start = 0; // THE END
	}
	
}else{
	
	$start = 0; // THE END
	return false;
		
}
}while($start > 0);

return true;

}


}